package com.yunfei.schedule;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yunfei.bo.*;
import com.yunfei.contants.FundFeneSureBookContants;
import com.yunfei.entity.LogContractGetEntity;
import com.yunfei.entity.TCustBuyinEntity;
import com.yunfei.entity.TSpciProdEntity;
import com.yunfei.entity.TblobstorageEntity;
import com.yunfei.mapper.test1.TblobtSorageMapper;
import com.yunfei.mapper.test2.TCustBuyinMapper;
import com.yunfei.mapper.test2.TLogContractGetMapper;
import com.yunfei.mapper.test2.TSpciProdMapper;
import com.yunfei.service.SchedualServiceHi;
import com.yunfei.utils.ComponentFactory;

import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
/**
 * 下线定时任务，用service_electronic_signature服务替代
 */
/*@Component*/
public class ScheduledTask {
    private static final org.slf4j.Logger Logger = LoggerFactory.getLogger(ScheduledTask.class);

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-DD HH:mm:ss");

    /**
     * 每隔5秒执行, 单位：ms。
     */
  /*  @Scheduled(fixedRate = 5000)
    public void testFixRate() {
        System.out.println("我每隔5秒冒泡一次：" + dateFormat.format(new Date()));
        Logger.info("打印当前时间: {}.", dateFormat.format(new Date()));
    }*/

 // @Scheduled(cron = "0 0 1 * * ?")   //每天凌晨1点执行
   @Scheduled(cron = "0 0 */2 * * ?")    //每1分钟
   /*@Scheduled(fixedRate = 50000000)*/
    public void testMyBatis() {
        System.out.println("我是每天凌晨1点开始执行");
        try {

            TCustBuyinMapper tCustBuyinMapper = ComponentFactory.getBean(TCustBuyinMapper.class);
            TblobtSorageMapper tblobtSorageMapper = ComponentFactory.getBean(TblobtSorageMapper.class);
            TLogContractGetMapper tLogContractGetMapper = ComponentFactory.getBean(TLogContractGetMapper.class);
            TSpciProdMapper tSpciProdMapper = ComponentFactory.getBean(TSpciProdMapper.class);
            List<TCustBuyinEntity> list = tCustBuyinMapper.findAllCreateSign();
            List<FundFeneSureBook> fundFeneSureBookList=null;
            System.out.println("需要生成认购书的总数===================================="+list.size());
            Gson gson=new Gson();
            if(list.size()>0){
                fundFeneSureBookList=new ArrayList<>();
                for(int i=0;i<list.size();i++){
                    TCustBuyinEntity custBuyinEntity=list.get(i);
                    System.out.println("认购书信息==========="+gson.toJson(custBuyinEntity));
                    TCustBuyinBo custBuyinBo=TCustBuyinBo.entityToBo(custBuyinEntity);
                    FundFeneSureBook fundFeneSureBook=TCustBuyinBo.getFeneBook(custBuyinBo);
                    //根据产品编号查询生成的认购书或者追加书模板的名称，配置在T_SPCI_PROD表里面，此外需要将模板放在项目service_electronic_signatu的reresources下面
                    String prodId=custBuyinEntity.getProdId();
                    System.out.println("根据产品编号:"+prodId+"查询对应的模板名称start.........................");
                    TSpciProdBo tSpciProdBo=new TSpciProdBo();
                    tSpciProdBo.setProdId(prodId);
                    //tSpciProdBo.setSpciTye("1");
                    TSpciProdEntity tSpciProdEntity=tSpciProdMapper.findByBo(tSpciProdBo);
                    System.out.println("查询到的模板为====================="+tSpciProdEntity);
                    if(!StringUtils.isEmpty(tSpciProdEntity)){
                        //返回的需要生成认购书的类型，01表示生成申购书，02，表示生成追加书
                        String cyType=fundFeneSureBook.getCyType();
                        String spciType=tSpciProdEntity.getSpciTye();//
                        if(FundFeneSureBookContants.SPIC_TYP_STATUS_1.equals(spciType)){
                            //即要生成申购书，又要生成追加书，具体什么哪个，根据cyType判断，如果cyType=1,生成申购书，如果cyType=1,生成追加书
                            //申购书
                            if(FundFeneSureBookContants.CON_TYP_STATUS_01.equals(cyType)){
                                System.out.println("查询:"+prodId+"查询对应的模板名称为........................."+tSpciProdEntity.getPurLett());
                                fundFeneSureBook.setSourcePath(tSpciProdEntity.getPurLett());
                                fundFeneSureBook.setSourcePath(tSpciProdEntity.getAddPurLett());
                            }
                            //追加书
                            if(FundFeneSureBookContants.CON_TYP_STATUS_02.equals(cyType)){
                                System.out.println("查询:"+prodId+"查询对应的模板名称为........................."+tSpciProdEntity.getAddPurLett());
                                fundFeneSureBook.setSourcePath(tSpciProdEntity.getAddPurLett());
                            }
                        }else{
                            //只生成申购书
                            fundFeneSureBook.setSourcePath(tSpciProdEntity.getAddPurLett());
                        }

                    }
                    //查询crm附件序号
                    String lStorageid=tblobtSorageMapper.getTblobStorageSeq()+"";
                    fundFeneSureBook.setlStorageid(lStorageid);
                    fundFeneSureBookList.add(fundFeneSureBook);
                }
            }
            SchedualServiceHi schedualServiceHi = ComponentFactory.getBean(SchedualServiceHi.class);
           String jsonData= gson.toJson(fundFeneSureBookList);
          // String jsonData= JSONUtils.toJSONString(fundFeneSureBook);
            System.out.println("开始去调用生成认购书的接口。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。");
            String resultJson=schedualServiceHi.sayHiFromClientOne(jsonData);
            System.out.println("调用生成认购书服务返回结果=============="+resultJson);
            //处理返回结果
            List <FundFeneSureBook> resultBackList=gson.fromJson(resultJson,new TypeToken<List<FundFeneSureBook>>(){}.getType());
            //定时任务可以做耗时操作，包括做生成数据库报表、文件IO等等需要定时执行的逻辑
            if (list != null) {
                System.out.println("一共多少条数据======================"+list.size());
                for(int i=0;i<resultBackList.size();i++){
                    FundFeneSureBook fundFeneSureBook=resultBackList.get(i);
                    //更新状态
                    String pkId=fundFeneSureBook.getPkId();
                    String storId=fundFeneSureBook.getlStorageid();
                    TCustBuyinEntity updateParam=new TCustBuyinEntity();
                    updateParam.setPkId(pkId);
                    updateParam.setContractId("Y");
                    tCustBuyinMapper.update(updateParam);
                    System.out.println("定时任务执行job"+(i+1)+":更新任务书状态ok" );
                    //插入日志
                    LogContractGetEntity logContractGetBo=new LogContractGetEntity();
                    logContractGetBo.setBuyinPkId(pkId);
                    logContractGetBo.setStorId(storId);
                    //系统日期 YYMMDD
                    tLogContractGetMapper.add(logContractGetBo);
                    System.out.println("定时任务执行job"+(i+1)+":插入日志ok" );
                    //生成附件表到Crmku
                    TblobstorageEntity tblobstorageBo=new TblobstorageEntity();
                    tblobstorageBo.setLStorageid(storId+"");
                    //合同号
                    String htNo=fundFeneSureBook.getHtNo();
                    tblobstorageBo.setCNodecode(htNo);
                    //生成的文件名
                    String fileName=fundFeneSureBook.getFileName();
                    String fileSize=fundFeneSureBook.getFileSize();
                    tblobstorageBo.setCFilename(fileName);
                    tblobstorageBo.setLFilesize(fileSize);
                    tblobtSorageMapper.add(tblobstorageBo);
                    System.out.println("定时任务执行job"+(i+1)+":生成crm附件表ok" );
                }
                System.out.println(list.size()+"条任务处理完成");

            } else {
                System.out.println("我什么事都不用做，等待下次再说吧");
            }
        } catch (Exception ex) {

        } finally {

        }
    }
}

